package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.AccountCustomer;
import com.briup.jz.bean.AccountCustomerExample;
import com.briup.jz.bean.AccountCustomerExample.Criteria;
import com.briup.jz.dao.AccountCustomerMapper;

import com.briup.jz.service.AccountCustomerService;
import com.briup.jz.utils.CustomerException;

@Service
public class AccountCustomerServiceImpl implements AccountCustomerService {

	@Autowired
	private AccountCustomerMapper accountCustomerMapper;

	//保存或者更新
	@Override
	public void saveOrUpdate(AccountCustomer accountCustomer) {
		if (accountCustomer.getId() != null) {
			// 更新修改的时间
			accountCustomer.setTransferTime(new Date().getTime());
			accountCustomerMapper.updateByPrimaryKey(accountCustomer);
		} else {
			// 新增修改的时间
			accountCustomer.setTransferTime(new Date().getTime());
			accountCustomerMapper.insert(accountCustomer);
		}

	}

	@Override
	public void deleteById(long id) throws CustomerException {
		AccountCustomer accountCustomer = accountCustomerMapper.selectByPrimaryKey(id);
		if (accountCustomer == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		accountCustomerMapper.deleteByPrimaryKey(id);

	}

	@Override
	public AccountCustomer queryAccountCustomerById(long id) {

		return accountCustomerMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<AccountCustomer> query(String type, String status, Long userId) {
		AccountCustomerExample example = new AccountCustomerExample();
		Criteria criteria = example.createCriteria();
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (userId != null) {
			criteria.andUserIdEqualTo(userId);
		}

		List<AccountCustomer> list = accountCustomerMapper.selectByExample(example);
		return list;
	}

}
